package borge.web.teamspirit.client.page.shared.widgets;

import java.util.ArrayList;
import java.util.List;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.DataProvider;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.page.team.TokenData;
import borge.web.teamspirit.client.servercomm.DBQueryResults;
import borge.web.teamspirit.client.servercomm.DBQueryResults.Result;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class ListDataWidget<T> extends BaseWidget implements ClickHandler, DataProvider {
	
	protected VerticalPanel iPanel;
	protected FlexTable iTable;
	//protected List<Button> iEditButtons;
	protected List<Button> iDeleteButtons;
	//protected List<Button> iShowButtons;
	//protected WidgetEventType iEditType;
	protected WidgetEventType iDeleteType;
	//protected WidgetEventType iShowType;
	protected boolean iHasButtons;
	//protected int iEditIndex;
	protected int iDeleteIndex;
	//protected int iShowIndex;
	protected List<T> iData;
	protected DataKey iDataKey;
	
	protected String iEditToken;
	protected String iShowToken;
	
	protected ListDataWidget(WidgetEventHandler aEventHandler, List<T> aData, DataKey aDataKey,
	        String aEditToken, WidgetEventType aDeleteType, String aShowToken){
		this();
		iData = aData;
		iDataKey = aDataKey;
		//iEditType = aEditType;
		iDeleteType = aDeleteType;
		//iShowType = aShowType;
		//iEditButtons = new ArrayList<Button>();
		iDeleteButtons = new ArrayList<Button>();
		//iShowButtons = new ArrayList<Button>();
		iEditToken = aEditToken;
		iShowToken = aShowToken;
		iHasButtons = true;
		setWidgetEventHandler(aEventHandler);
		
	}
	
	protected ListDataWidget(WidgetEventHandler aEventHandler, List<T> aData){
		this();
		setWidgetEventHandler(aEventHandler);
		iData = aData;
	}
	
	protected ListDataWidget(){
		iPanel = new VerticalPanel();
		iTable = new FlexTable();
		iPanel.add(iTable);
		reset();
	}
	
	protected void reset(){
    	
		//iPanel.clear();
		
		iTable.removeAllRows();
		if(iHasButtons){
			//iEditButtons.clear();
			iDeleteButtons.clear();
			//iShowButtons.clear();
		}
	}
	
	public void update(List<T> aData){
		iData = aData;
		createDataTable(); // calls reset()
	}
	
    public void onClick(ClickEvent e){
        
    	Button b = (Button)e.getSource();
    		/*
    	if(b.getText().equals(TeamSpirit.getConstants().edit())){
    		
    		for(int i = 0; i < iEditButtons.size(); ++i){
    			
    			if(b == iEditButtons.get(i)){
    				iEditIndex = i;
    				//postEvent(iEditType);
    			}
    		}
    	}
    	else if(b.getText().equals(TeamSpirit.getConstants().show())){
    	    
           for(int i = 0; i < iShowButtons.size(); ++i){
                
                if(b == iShowButtons.get(i)){
                    iShowIndex = i;
                    //postEvent(iShowType);
                    History.newItem(TokenData.assembleToken(TokenData.SHOW, TokenData.TEAM, Integer.toString(i)));
                }
            }
    	}*/
    	//else{
    		
    		for(int i = 0; i < iDeleteButtons.size(); ++i){
    			
    			if(b == iDeleteButtons.get(i)){
    				iDeleteIndex = i;
    				//postEvent(iDeleteType);
    			}
    		}
    	//}
    	
    }
    
    //public abstract void objectDeleted();
    protected abstract void createDataTable();
    
	public void dataUpdate(DBQueryResults aResults) {
		
		//Window.alert("dataupdate");
		Result result = aResults.getResult(iDataKey);
		if(result != null){
			List<T> data = (List<T>)result.getResultList();
			if(data != null && data.size() > 0){
				iData = data;
				createDataTable();
			}	
		}
	}
    
    protected void addButtons(int row, int col, String id){
    
    	if(!iHasButtons)
    		return;
    	
    	Button remove = new Button(TeamSpirit.getConstants().delete());
        //Button edit = new Button(TeamSpirit.getConstants().edit());
        //Button show = new Button(TeamSpirit.getConstants().show());
        
        remove.addClickHandler(this);
        //edit.addClickHandler(this);
        //show.addClickHandler(this);
        
        iTable.setWidget(row, col++, new Hyperlink(TeamSpirit.getConstants().show(), 
        		TokenData.addTokenData(iShowToken, id)));
        iTable.setWidget(row, col++, new Hyperlink(TeamSpirit.getConstants().edit(), 
        		TokenData.addTokenData(iEditToken, id)));
        //iTable.setWidget(row, col++, show);
        //iTable.setWidget(row, col++, edit);
        iTable.setWidget(row, col++, remove);

        //iEditButtons.add(edit);
        iDeleteButtons.add(remove);
        //iShowButtons.add(show);
    }
    
    @SuppressWarnings("unchecked")
	public T getDataObject(){
       
    	//  clear indexes before returning, except for delete?
//        if(iEditIndex != -1)
//            return iData.get(iEditIndex);
//        else if(iDeleteIndex != -1)
//            return iData.get(iDeleteIndex);
//        else if(iShowIndex != -1)
            return iData.get(iDeleteIndex);
        
       
    }

    
    public void objectDeleted(){
    	iData.remove(iDeleteIndex);
    	iDeleteIndex = -1;
    	createDataTable();
    }
    
    public boolean needsServerUpdate(){
    	return iData == null;
    }
    
    public abstract void requestServerData(ServerCallbackHandler aHandler);
    
    //public abstract void handleTokenData(String aData);
    
}
